#pragma once
#include <iostream>
#include <string>
#include <atomic>
#include <vector>
#include <cstring>
#include <chrono>
#include <cstdint>

// 订单类型枚举
enum class OrderType : uint8_t
{
    MARKET, // 市价单
    LIMIT   // 限价单
};

enum class OrderDirection : uint8_t
{
    BUY = 0,
    SELL = 1
};

static constexpr size_t ACCOUNT_ID_SIZE = 32;
static constexpr size_t ORDER_ID_SIZE = 32;
static constexpr size_t SYMBOL_SIZE = 16;

// 订单结构体
struct OrderPacket
{
    uint8_t accountId[ACCOUNT_ID_SIZE]{};
    uint8_t orderId[ORDER_ID_SIZE]{};
    uint8_t symbol[SYMBOL_SIZE]{};
    OrderDirection isBuy = OrderDirection::BUY;
    OrderType orderType = OrderType::LIMIT;
    uint64_t price = 0;
    uint32_t quantity = 0;
    uint64_t timestamp = 0;

    // 默认构造函数
    OrderPacket() = default;

    // 参数构造函数
    OrderPacket(const std::string &accId, const std::string &ordId, const std::string &sym,
                bool buy, OrderType type, double p, uint32_t q)
        : isBuy(buy ? OrderDirection::BUY : OrderDirection::SELL),
          orderType(type),
          price(static_cast<uint64_t>(p * 1000)),
          quantity(q),
          timestamp(std::chrono::duration_cast<std::chrono::milliseconds>(
                        std::chrono::system_clock::now().time_since_epoch())
                        .count())
    {
        std::memset(accountId, 0, ACCOUNT_ID_SIZE);
        std::memset(orderId, 0, ORDER_ID_SIZE);
        std::memset(symbol, 0, SYMBOL_SIZE);
        std::memcpy(accountId, accId.c_str(), std::min(accId.size(), ACCOUNT_ID_SIZE));
        std::memcpy(orderId, ordId.c_str(), std::min(ordId.size(), ORDER_ID_SIZE));
        std::memcpy(symbol, sym.c_str(), std::min(sym.size(), SYMBOL_SIZE));
    }

    // 拷贝构造函数
    OrderPacket(const OrderPacket &other)
        : isBuy(other.isBuy),
          orderType(other.orderType),
          price(other.price),
          quantity(other.quantity),
          timestamp(other.timestamp)
    {
        std::memcpy(accountId, other.accountId, ACCOUNT_ID_SIZE);
        std::memcpy(orderId, other.orderId, ORDER_ID_SIZE);
        std::memcpy(symbol, other.symbol, SYMBOL_SIZE);
    }

    // 移动构造函数
    OrderPacket(OrderPacket &&other) noexcept
        : isBuy(other.isBuy),
          orderType(other.orderType),
          price(other.price),
          quantity(other.quantity),
          timestamp(other.timestamp)
    {
        std::memcpy(accountId, other.accountId, ACCOUNT_ID_SIZE);
        std::memcpy(orderId, other.orderId, ORDER_ID_SIZE);
        std::memcpy(symbol, other.symbol, SYMBOL_SIZE);
    }

    // 拷贝赋值运算符
    OrderPacket &operator=(const OrderPacket &other)
    {
        if (this != &other)
        {
            std::memcpy(accountId, other.accountId, ACCOUNT_ID_SIZE);
            std::memcpy(orderId, other.orderId, ORDER_ID_SIZE);
            std::memcpy(symbol, other.symbol, SYMBOL_SIZE);
            isBuy = other.isBuy;
            orderType = other.orderType;
            price = other.price;
            quantity = other.quantity;
            timestamp = other.timestamp;
        }
        return *this;
    }

    // 移动赋值运算符
    OrderPacket &operator=(OrderPacket &&other) noexcept
    {
        if (this != &other)
        {
            std::memcpy(accountId, other.accountId, ACCOUNT_ID_SIZE);
            std::memcpy(orderId, other.orderId, ORDER_ID_SIZE);
            std::memcpy(symbol, other.symbol, SYMBOL_SIZE);
            isBuy = other.isBuy;
            orderType = other.orderType;
            price = other.price;
            quantity = other.quantity;
            timestamp = other.timestamp;
        }
        return *this;
    }
};
